// import userApi from "@/api/pond-patrol-managementbasic-info-manage/user";

import MaterialApi from "@/api/basic-info-manage/material";
import BatchApi from "@/api/batch-mange";

const getDefaultState = () => {
    return {
        material: {},
        batch: {}
    };
};

const state = getDefaultState();

const mutations = {
    SET_MATERIAL: (state, data) => {
        state.material = data;
    },
    SET_BATCH: (state, data) => {
        state.batch = data;
    },
};

const actions = {
    getMaterial({ commit }) {
        return new Promise((resolve, reject) => {
            MaterialApi.queryByCondition({}).then(res => {
                let data = {}
                if (res.success && res.data) {
                    res.data.forEach(item => {
                        data[item.materialId] = {
                            name: item.name || '',//物料名称
                            unit: item.basicUnitName || '',//单位
                            aUnit: item.auxiliaryUnitName || '',//单位,
                            brand: item.brand || '', //规格单位
                            ratio: `${item.ratio || ''}${item.basicUnitName}${item.auxiliaryUnitName ? '/' + item.auxiliaryUnitName : ''}`, //规格
                            ratioNum: item.ratio || ''
                        }
                    });
                }
                // console.log('**********', data)
                commit("SET_MATERIAL", data);
            }).catch(err => {
                commit("SET_MATERIAL", {});
            })
        })
    },
    getBatch({ commit }) {
        return
        return new Promise((resolve, reject) => {
            BatchApi.queryByCondition({}).then(res => {
                let data = {}
                if (res.success && res.data) {
                    res.data.forEach(item => {
                        data[item.mnecode] = data[item.productionBatchId] = {
                            batchName: item.mnecode || '',//批次编号
                            batchId: item.productionBatchId || '',//批次id
                            batchCode: item.productionBatchCode || '',//批次编号
                            breedTypeName: item.breedTypeName || '',//养殖类型
                            breedTypeName: item.breedTypeName || '',//养殖类型
                            varietyName: item.varietyName || '',//养殖品种
                            inputNum: item.inputNum || 0,
                            avgWeight: item.avgWeight || 0,
                            weight: (item.inputNum || 0) * (item.avgWeight || 0)
                        }
                    });
                }
                // console.log('**********', data)
                commit("SET_BATCH", data);
            }).catch(err => {
                commit("SET_BATCH", {});
            })
        })
    },
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};
